↜ Back to index Introduction to Numerical Analysis 1

Solution Lecture a3

Exercise 1

! Implementation of the Euler method for y' = f(t, y)
implicit none
integer i, n
real y, h, t, f

n = 10
h = 1. / n      ! note 1. and not 1

! Initial data
y = 1.
print *, 0., y

do i = 1, n
    ! One step of the Euler method.
    t = (i - 1) * h
    ! f(t, y)
    f = y
    y = y + h * f
    print *, i * h, y
enddo
end

Exercise 2

! Implementation of the Euler method for y' = f(t, y)
implicit none
integer i, n
real y, h, t, f

n = 45
h = 1. / n      ! note 1. and not 1

! Initial data
y = 1.
print *, 0., y

do i = 1, n
    ! One step of the Euler method.
    t = (i - 1) * h
    ! f(t, y)
    f = -100 * y
    y = y + h * f
    print *, i * h, y
enddo
end

Exercise 3

! Implementation of the Euler method for y' = f(t, y)
implicit none
integer i, n
real y, h, t, f

n = 20
! interval (0, 5)
h = 5. / n

! Initial data
y = 1.
print *, 0., y

do i = 1, n
    ! One step of the Euler method.
    t = (i - 1) * h
    ! f(t, y)
    f = y * sin(t)
    y = y + h * f
    print *, i * h, y
enddo
end